package com.mcm.modelos;

import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import com.mcm.dto.Producto;
import com.mcm.dto.ProductoVenta;

public class ModeloVenta extends AbstractTableModel implements TableModelListener
{
	private static final long serialVersionUID = 3395990678215457645L;
	
	private String[] columnNames = {"#",
			"Producto",
			"Precio",
			"Cantidad",
			"Sub Total"};
	private final List<ProductoVenta> list;
	
	public ModeloVenta(List<ProductoVenta> lista)
	{
		this.addTableModelListener(this);
		list = lista;
	}

    public List<ProductoVenta> getList() {
		return list;
	}

	public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return list.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    	switch(col) {
        case 0: return list.get(row).getNroFila();
        case 1: return list.get(row).getDescripcion();
        case 2: return list.get(row).getPrecioVenta();
        case 3: return list.get(row).getCantVenta();
        case 4: return list.get(row).getPrecioTotal();
	    }
	    return null;
    }
    @Override
    public void setValueAt(Object value, int row, int col) {
        if(col==3 && ((String)value).equals(""))
        	return;
    	switch(col) {
	        case 0: list.get(row).setNroFila(Integer.valueOf((String)value));break;
	        case 1: list.get(row).setDescripcion((String)value);break;
	        case 2: list.get(row).setPrecioVenta(Double.valueOf((String)value));break;
	        case 3: list.get(row).setCantVenta(Integer.valueOf((String)value));break;
	        case 4: list.get(row).setPrecioTotal((Double)value);break;
        }
        fireTableCellUpdated(row, col);
    }

    public boolean isCellEditable(int row, int col) {
        if(col==3)
        	return true;
        else
        	return false;
    }

    public void addElement(ProductoVenta p) {
        list.add(p);
        fireTableRowsInserted(list.size()-1, list.size()-1);
    }
    
    public void removeElement(Producto p)
    {
    	list.remove(p);
    	fireTableRowsDeleted(6, 6);
    }
    
    public ProductoVenta getElementRow(int row)
    {
    	return list.get(row);
    }    
    public void tableChanged(TableModelEvent e) 
    {
    	int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        if(column == 3)
        {
        	Integer nuevaCantidad = (Integer)model.getValueAt(row, column);
	        Double precio = (Double)model.getValueAt(row, 2);
	        if(nuevaCantidad==null)
	        	return;
	        
	        Double nuevoSubtotal = nuevaCantidad * precio;
	        model.setValueAt(nuevoSubtotal,row, 4);
        }
//        System.out.println("Grabo los cambios");
    }
    
    public Double getTotal()
    {
    	Double total = 0d;
    	for(ProductoVenta pv : list)
    	{
    		total += pv.getPrecioTotal();
    	}
    	return total;
    }
}